package com.example.zxinglibrary;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private static final int CaptureActivityResult = 100;//扫描二维码

    private TextView txvResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Log.d("MyDebug", "MainActivity.onCreate");

        setContentView(R.layout.activity_main);

        Button btnScan = (Button) findViewById(R.id.btnScan);
        btnScan.setOnClickListener(this);

        txvResult = (TextView) findViewById(R.id.txvResult);
    }

    @Override
    public void onClick(View v) {
        Log.d("MyDebug", "MainActivity.onClick: " + v.toString());
        switch (v.getId()) {
            case R.id.btnScan:
                Scan();
                break;

            default:
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {
            case CaptureActivityResult:
                String result = "未识别";
                if (resultCode == RESULT_OK) {
                    result = data.getStringExtra("result");
                } else {
                    result = "取消识别";
                }
                txvResult.setText(result);

                break;

            default:
        }
    }

    private void Scan() {
        Intent intent = new Intent(MainActivity.this, CaptureActivity.class);
        //Intent intent = new Intent(MainActivity.this, MyCaptureActivity.class);
        startActivityForResult(intent, CaptureActivityResult);
    }
}
